#!/bin/bash

# show_grd.sh: Display information and visualization for a GMT GRD file
# Usage: ./show_grd.sh grd_file
# Requires a GRD file as input argument
# build by luke 

# Check if GMT is installed
if ! command -v gmt &> /dev/null; then
    echo "Error: GMT is not installed. Please install GMT first."
    exit 1
fi

# Check if GRD file is provided
if [ -z "$1" ]; then
    echo "Error: No GRD file specified."
    echo "Usage: $0 grd_file"
    exit 1
fi

# Set GRD file from argument
GRD_FILE="$1"

# Check if GRD file exists
if [ ! -f "$GRD_FILE" ]; then
    echo "Error: $GRD_FILE does not exist in the current directory."
    exit 1
fi

# Get base name of GRD file (without extension)
BASENAME=$(basename "$GRD_FILE" .grd)

# 1. Display GRD file information and statistics
echo "=== GRD File Information for $GRD_FILE ==="
gmt grdinfo "$GRD_FILE" -L2
echo ""

# 2. Create visualization
echo "Generating visualization for $GRD_FILE..."
gmt begin "$BASENAME" png
    # Create image with jet colormap, 10 cm width, geographic projection
    gmt grdimage "$GRD_FILE" -Cjet -JM10c -B -BWSne+t"$BASENAME"
    # Add colorbar
    gmt colorbar -DJBC -Bxa -By+l"Value"
gmt end show

echo "Visualization saved as $BASENAME.png"
echo "Done!"
